package com.example.dirviesystem.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Component
public class AliOSSutils {
    String endpoint ="oss-cn-chengdu.aliyuncs.com";
    String accessKeyId ="LTAI5t8B9z3Cv5H8vqBRkeh2";
    String accessKeySecret = "9ci1R5MKn6q1pW6Y4opVD8j5OulgJp";
    String bucketName = "goudan-00";
    public String upload(MultipartFile file) throws IOException {
       InputStream inputStream=file.getInputStream();
       String fileName= "publicPlan/"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"))+file.getOriginalFilename();
       OSS ossCilent=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
       ossCilent.putObject(bucketName,fileName,inputStream);
        String url ="https://"+bucketName+"."+endpoint+"/"+fileName;
        ossCilent.shutdown();
        return url;
    }
}
